package com.zjw.zy.base

import android.graphics.Color
import android.graphics.drawable.Drawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.view.WindowManager
import android.widget.EditText
import androidx.annotation.ColorRes
import androidx.core.graphics.ColorUtils
import androidx.core.graphics.drawable.toBitmap
import androidx.palette.graphics.Palette
import com.blankj.utilcode.util.KeyboardUtils
import com.zjw.zy.R
import com.zjw.zy.ext.byHint
import com.zjw.zy.ext.edit
import com.zjw.zy.ext.focus
import com.zjw.zy.ext.normal
import com.zjw.zy.utils.ResourcesUtils
import com.zjw.zy.utils.WindowUtils

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/9/26 9:51
 */
abstract class BaseActivity : AppCompatActivity() {

    private var systemUiVisibility = 0

    companion object {
        val IGNORE_KEYBOARD_STATUE = "BaseActivity_IGNORE_KEYBOARD_STATUE".hashCode()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(onLayoutId())
        onInit()
        onFindView()
        onBindValue()
        onBindListener()
    }

    private fun onInit() {
        systemUiVisibility = window.decorView.systemUiVisibility
    }

    abstract fun onBindListener()

    abstract fun onBindValue()

    abstract fun onFindView()

    protected abstract fun onLayoutId(): Int

    /**
     * 沉浸式
     */
    fun immersive() {
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        transparentStatusBar()
    }

    /**
     * 反沉浸式
     */
    fun unImmersive() {
        window.decorView.systemUiVisibility = systemUiVisibility
        restoreStatusBar()
    }

    /**
     * 透明状态栏
     * */
    fun transparentStatusBar() {
        window.statusBarColor = Color.TRANSPARENT
    }

    /**
     * 还原状态栏
     * */
    fun restoreStatusBar() {
        window.statusBarColor = ResourcesUtils.getColor(R.color.top_bar_main_color)
    }

    /**
     * 设置全屏背景
     * */
    fun setFullBackGround(drawable: Drawable) {
        transparentStatusBar()
        window.decorView.background = drawable

        Palette.from(drawable.toBitmap())
            .setRegion(0, 0, 100, 100)
            .generate {
                it?.swatches?.maxByOrNull { switch -> switch.population }?.let { switch ->
                    ColorUtils.calculateLuminance(switch.rgb).let { luminance ->
                        if (luminance <= 0.5) {
                            //深色
                            val flags =
                                window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                            window.decorView.systemUiVisibility =
                                flags xor View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR

                        } else {
                            //浅色
                            val flags = window.decorView.systemUiVisibility
                            window.decorView.systemUiVisibility =
                                flags or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                        }
                    }
                }
            }
    }

    fun setFullBackGround(@ColorRes id: Int) {
        transparentStatusBar()
        window.decorView.background = null
        val color = ResourcesUtils.getColor(id)
        window.decorView.setBackgroundColor(color)

        ColorUtils.calculateLuminance(color).let { luminance ->
            if (luminance <= 0.5) {
                //深色
                val flags =
                    window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                window.decorView.systemUiVisibility =
                    flags xor View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR

            } else {
                //浅色
                val flags = window.decorView.systemUiVisibility
                window.decorView.systemUiVisibility =
                    flags or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            }
        }
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        WindowUtils.PopupWindow.hide()
        val consume = super.dispatchTouchEvent(ev)
        if (ev?.action == MotionEvent.ACTION_DOWN) {
            val focus = currentFocus
            if (focus != null) {
                dispatchSoftKeyboardShow(focus, ev)
            }
        }
        return consume
    }

    private fun dispatchSoftKeyboardShow(view: View, ev: MotionEvent) {
        val ignore = view.getTag(IGNORE_KEYBOARD_STATUE)
        if (ignore != null) {
            return
        }
        if (view is EditText && view.byHint(ev)) {
            view.isCursorVisible = true
            KeyboardUtils.showSoftInput()
        } else {
            KeyboardUtils.hideSoftInput(this)

            if (view is EditText) {
                //close focus
                view.isCursorVisible = false
            }
        }
    }
}
